{- This file is part of funbot.
 -
 - Written in 2017 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

{-# LANGUAGE OverloadedStrings #-}

-- | ignore, unignore
--
-- Manage ignored users
module FunBot.Commands.Access
    ( cmdIgnore
    , cmdUnignore
    )
where

import Data.Text (Text)
import Network.IRC.Fun.Bot.Chat
import Network.IRC.Fun.Bot.Types
import Network.IRC.Fun.Types.Base

import FunBot.Settings.Sections.Access
import FunBot.Types
import FunBot.Util (cmds, helps)

respondIgnore
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondIgnore _mchan sender [arg] send = do
    a <- isAngel sender
    if a
        then do
            let demon = Nickname arg
            d <- isDemon demon
            if d
                then send $ MsgContent "User is already ignored"
                else do
                    addDemon demon
                    send $ MsgContent "User ignored"
        else send $ MsgContent "You can’t modify ignored the user list."
respondIgnore mchan nick args _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) (Just 1)

cmdIgnore = Command
    { cmdNames    = cmds ["ignore"]
    , cmdRespond  = respondIgnore
    , cmdHelp     = helps
        [ ( "ignore <nickname>"
          , "ask me to ignore commands from a given user"
          )
        ]
    , cmdExamples =
        [ "ignore evil_person"
        ]
    }

respondUnignore
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondUnignore _mchan sender [arg] send = do
    a <- isAngel sender
    if a
        then do
            let demon = Nickname arg
            d <- isDemon demon
            if d
                then do
                    removeDemon demon
                    send $ MsgContent "User unignored"
                else send $ MsgContent "User already isn’t ignored"
        else send $ MsgContent "You can’t modify ignored the user list."
respondUnignore mchan nick args _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) (Just 1)

cmdUnignore = Command
    { cmdNames    = cmds ["unignore"]
    , cmdRespond  = respondUnignore
    , cmdHelp     = helps
        [ ( "unignore <nickname>"
          , "ask me to stop ignoring commands from a given user"
          )
        ]
    , cmdExamples =
        [ "unignore evil_person"
        ]
    }
